﻿Imports System.Windows.Forms

Public Class Dialog5

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        Dim insert As Boolean = False
        Dim JLID As String = ""

        Dim CADISCon As New System.Data.SqlClient.SqlConnection(My.Settings.CADISConnectionString)
        Dim CADISCon1 As New System.Data.SqlClient.SqlConnection(My.Settings.CADISConnectionString)

        Dim CADISCmd As New System.Data.SqlClient.SqlCommand
        Dim CADISCmd1 As New System.Data.SqlClient.SqlCommand

        Dim CADISReader As SqlClient.SqlDataReader = Nothing


        Dim EASTAT As String = "N"
        Dim EAEMNO As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EAEMNO")

        Dim EADIVI As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EADIVI")

        Dim EACNTRY As String = ""
        'Dim EACNTRY As String = "CH"
        ' If My.Settings.Env = "M001" Then EACNTRY = "CT" Else EACNTRY = "CH"
        EACNTRY = My.Settings.CadisEnv

        'Setings for CH
        Dim EACOMP As String = "03965"
        Dim EAREGN As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EAREAR")
        EAREGN = EAREGN.Substring(3, 5)
        'Seting for other
        If EADIVI <> "103" Then
            EAREGN = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EAREAR")
            EACOMP = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EAFACI")
        End If

        Dim EAEMNM As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EAEMNM")
        Dim EAPASWD As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EAEMNO")
        Dim EAWHLO As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EAWHLO")
        Dim EATEL As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EACAPH")
        Dim EAMOBL As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EACAPH1")
        Dim EAEMAIL As String = ""
        Dim EALANG As String = ""

        If RadioButton1.Checked Then
            EALANG = "DE"
        Else
            EALANG = "FR"
        End If

        Dim EASRES As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EASRES")
        Dim EADEPT As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EADEPT")
        Dim EAREAR As String = Technicien.GridView2.GetRowCellValue(Technicien.GridView2.FocusedRowHandle, "EAREAR")


        CADISCmd.Connection = CADISCon
        CADISCmd1.Connection = CADISCon1
        CADISCon.Open()
        CADISCon1.Open()

        CADISCmd.CommandText = "SELECT JLID FROM F0010 WHERE EAEMNO = " & EAEMNO
        CADISReader = CADISCmd.ExecuteReader


        If CADISReader.Read Then
            JLID = Trim(CADISReader(0).ToString())

            CADISCmd1.CommandText = "UPDATE dbo.F0010 SET  " & _
                                    "EAEMNO = " & EAEMNO & ", " & _
                                    "EACNTRY = '" & EACNTRY & "', " & _
                                    "EACOMP = '" & EACOMP & "', " & _
                                    "EAREGN = '" & EAREGN & "', " & _
                                    "EAEMNM = '" & EAEMNM & "', " & _
                                    "EAPASWD =' " & EAPASWD & "', " & _
                                    "EAWHLO = '" & EAWHLO & "', " & _
                                    "EATEL = '" & EATEL & "', " & _
                                    "EAMOBL = '" & EAMOBL & "', " & _
                                    "EAEMAIL = '" & EAEMAIL & "', " & _
                                    "EALANG = '" & EALANG & "', " & _
                                    "EASRES = '" & EASRES & "', " & _
                                    "EADEPT = '" & EADEPT & "', " & _
                                    "EAREAR = '" & EAREAR & "' " & _
                                    "WHERE JLID = " & JLID
            CADISCmd1.ExecuteNonQuery()

        Else
            CADISCmd1.CommandText = "INSERT INTO dbo.F0010(EASTAT, EAEMNO, EACNTRY, EACOMP, EAREGN, " & _
                                                          "EAEMNM, EAPASWD, EAWHLO, EATEL, EAMOBL, EAEMAIL,  " & _
                                                          "EALANG, EASRES, EADEPT, EAREAR) " & _
                                                          "VALUES ('" & EASTAT & "', " & EAEMNO & ", '" & EACNTRY & "', '" & EACOMP & "', '" & EAREGN & "', " & _
                                                          "'" & EAEMNM & " ', '" & EAPASWD & "', '" & EAWHLO & "', '" & EATEL & "', '" & EAMOBL & "', '" & EAEMAIL & "', " & _
                                                          "'" & EALANG & " ', '" & EASRES & "', '" & EADEPT & "', '" & EAREAR & "')"
            CADISCmd1.ExecuteNonQuery()
        End If

        CADISCon.Close()


        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

End Class
